//
//  NSDictionary+ForNSNull.m
//  XueLeTS
//
//  Created by Sheldon huang on 12/7/15.
//  Copyright © 2015 net.xuele. All rights reserved.
//

#import "NSDictionary+ForNSNull.h"

@implementation NSDictionary(forNSNull)
//处理json含有null的情况
- (id)objectForKeySafely:(id)aKey{
    if(self == nil || self == (id)[NSNull null]){
        NSLog(@"NSDictionary warning:nsdictionary为nil");
        return nil;
    }
    id value = [self objectForKey:aKey];
    if (value == nil || value == (id)[NSNull null]) {
        if(value == (id)[NSNull null]) NSLog(@"NSDictionary warning:(key=%@,value=NSNull)", aKey);
        return nil;
    }else
        return value;
}
@end

@implementation NSNull(forNSDictionary)
- (id)objectForKeySafely:(id)aKey{
    NSLog(@"NSDictionary warning:nsdictionary为NSNull key=%@", aKey);
    return nil;
}
@end

@implementation NSString(forEmpty)

-(BOOL)isEmpty
{
    return self == nil || [@"" isEqualToString:self] || [@"(null)" isEqualToString:self];
}

@end



@implementation NSMutableDictionary (SafeValue)

- (void)setSafeObject:(id)anObject forKey:(id<NSCopying>)aKey
{
    if (aKey) {
        if (!anObject) {
            [self setObject:[NSNull null] forKey:aKey];
        } else {
            [self setObject:anObject forKey:aKey];
        }
    }
}

@end

